#include "../include/head.h"
#include "../include/func.h"

int login_view(int sockfd, User_t *user) {
    int user_choice;
    while (1) {
        printf("\033[1;34m"); // 蓝色字体
        printf("+------------------------------------------------+\n");
        printf("|                                                |\n");
        printf("|                    WELCOME                     |\n");
        printf("|     ---- 'SUI YU ER AN 's CloudDisk' ----      |\n");
        printf("|                                                |\n");
        printf("+------------------------------------------------+\n");
        printf("\033[0m"); // 重置颜色
        printf("\n");

        printf("\n\033[1;96m    ----------- LOGIN MENU -----------\033[0m \n");
        printf("\033[1;97m    | Hello, please choose an option: |\033[0m\n");
        printf("\033[1;97m    |                                 |\033[0m\n");
        printf("\033[1;97m    |         1. Register             |\033[0m\n");
        printf("\033[1;97m    |         2. Login                |\033[0m\n");
        printf("\033[1;97m    |         3. Exit                 |\033[0m\n");
        printf("\033[1;97m    |                                 |\033[0m\n");
        printf("\033[1;96m    ----------------------------------\033[0m \n");
        printf("\033[1;97m    Your choice: \033[0m");

        // 判断用户是否输入了一个整数
        if (scanf("%d", &user_choice) != 1) {
            while (getchar() != '\n');
            printf("\033[1;96m    ----------------------------------\033[0m \n");
            printf("\033[1;97m    无效的输入，请输入数字！！！ \033[0m");
            continue;
        }
        while (getchar() != '\n');

        switch (user_choice) {
        case 1:
            register_client(sockfd);
            break;
        case 2:
            if (login_client(sockfd, user) == 0) {
                printf("登录成功！\n");
                return 0;   // 退出函数，登录成功
            } else {
                printf("登陆失败，请重试。\n");
            }
            break;
        case 3:
            printf("用户退出，再见！\n");
            exit(EXIT_SUCCESS);
        default:
            printf("输入错误，请重新输入！\n");
            break;
        }
    }
    return 0;
}
